package com.arpa.wms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.domain.entity.ListTrend;
import com.arpa.wms.mapper.ListTrendMapper;
import com.arpa.wms.service.IDeliveryService;
import com.arpa.wms.service.IInboundService;
import com.arpa.wms.service.IListTrendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 单量趋势统计(定时任务) 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2021-06-09
 */
@Service
public class ListTrendServiceImpl extends ServiceImpl<ListTrendMapper, ListTrend> implements IListTrendService {

    @Autowired
    private IInboundService iInboundService;
    @Autowired
    private IDeliveryService iDeliveryService;

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(ListTrend entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
        }
        return super.save(entity);
    }

    /**
     * 入库定时任务
     */
    @Override
    public void inboundTimingTrend() {
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00").format(LocalDateTime.now().plusDays(-1));
        String inboundTrend = iInboundService.inboundQuantityTrend("0", date, "0");
        ListTrend listTrend = new ListTrend();
        listTrend.setType(0);
        listTrend.setQuantity(inboundTrend);
        listTrend.setGmtCreated(LocalDateTime.now());
        this.save(listTrend);
    }

    /**
     * 出库定时任务
     */
    @Override
    public void outboundTimingTrend() {
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00").format(LocalDateTime.now().plusDays(-1));
        String outboundTrend = iDeliveryService.outboundQuantityTrend(date, "11");
        ListTrend listTrend = new ListTrend();
        listTrend.setType(1);
        listTrend.setQuantity(outboundTrend);
        listTrend.setGmtCreated(LocalDateTime.now());
        this.save(listTrend);
    }

    /**
     * 退货定时任务
     */
    @Override
    public void cancelTimingTrend() {
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00").format(LocalDateTime.now().plusDays(-1));
        String inboundTrend = iInboundService.inboundQuantityTrend("0", date, "1");
        ListTrend listTrend = new ListTrend();
        listTrend.setType(2);
        listTrend.setQuantity(inboundTrend);
        listTrend.setGmtCreated(LocalDateTime.now());
        this.save(listTrend);
    }


    /**
     * 入出退库统计(一个月)
     * @return
     */
    private List<String> orderQuantityTrend(String type) {
        return baseMapper.orderQuantityTrend(type);
    }

    /**
     * 处理List数据
     * @return
     */
    @Override
    public List<Object> processingData() {
        List<String> inboundTrend = this.orderQuantityTrend("0");
        if (inboundTrend.size() < 30){
            inboundTrend = disposeList(inboundTrend);
        }
        List<String> outboundTrend = this.orderQuantityTrend("1");
        if (outboundTrend.size() < 30){
            outboundTrend = disposeList(outboundTrend);
        }
        List<String> cancelTrend = this.orderQuantityTrend("2");
        if (cancelTrend.size() < 30){
            cancelTrend = disposeList(cancelTrend);
        }
        List list = new ArrayList();
        list.add(inboundTrend);
        list.add(outboundTrend);
        list.add(cancelTrend);
        return list;
    }

    private List<String> disposeList(List<String> str){
        int size = new Integer(30) - str.size();
        for (int i = 0; i<size; i++){
            str.add(0,"0");
        }
        return str;
    }
}
